package online.struts.chain.processor;

import java.util.Map;

import org.apache.struts.action.Action;
import org.apache.struts.chain.Constants;
import org.apache.struts.chain.commands.servlet.CreateAction;
import org.apache.struts.chain.contexts.ActionContext;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ModuleConfig;

import core.config.Env;
import core.config.Factory;

/**
 * アクション作成プロセッサ
 *
 * @author Tadashi Nakayama
 * @version 1.0.0
 */
public final class CreateActionProcessor extends CreateAction {

	/**
	 * @see org.apache.struts.chain.commands.AbstractCreateAction
	 * \#execute(org.apache.struts.chain.contexts.ActionContext)
	 */
	@Override
	public boolean execute(final ActionContext actionCtx) throws Exception {
		return actionCtx.getForwardConfig() == null && super.execute(actionCtx);
	}

	/**
	 * @see org.apache.struts.chain.commands.AbstractCreateAction
	 * #getAction(org.apache.struts.chain.contexts.ActionContext,
	 * java.lang.String, org.apache.struts.config.ActionConfig)
	 */
	@Override
	protected synchronized Action getAction(final ActionContext context, final String type,
			final ActionConfig actionConfig) throws Exception {
		Action action = super.getAction(context, type, actionConfig);
		if (action != null && Env.isDevelop()) {
			ModuleConfig moduleConfig = actionConfig.getModuleConfig();
			String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
			Map<String, Action> actions = Factory.cast(
					context.getApplicationScope().get(actionsKey));
			if (actions != null) {
				actions.remove(type);
			}
		}

		return action;
	}
}
